package cn.broadin.app.hotel.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageButton;

public class StrokeImageButton extends ImageButton {
	
	private String mText = "";
	private int mTextSize = 24;
	private int mTextColor = Color.WHITE;
	private Rect mRect = null;
	private PaintFlagsDrawFilter pfd;
	
	public StrokeImageButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
	}

	public void setText(String text,int text_size){
		mText = text;
		mTextSize = text_size;
	}
	public String getText(){
		return mText;
	}
	public void setTextColor(int r){
		mTextColor = r;
	}
	
	public void setDrawRect(Rect r){
		if(r != null && r.width() > 0 && r.height() >0 
				&& r.left < r.right && r.top < r.bottom){
			mRect  = r;
		}
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint=new Paint();
		mRect = null;
		Rect rec = (mRect == null) ? canvas.getClipBounds(): mRect;
		
        paint.setTextAlign(Paint.Align.CENTER);  
        paint.setTextSize(mTextSize);
        paint.setColor(mTextColor); 
		paint.setAntiAlias(true);// 消除锯齿
		canvas.setDrawFilter(pfd);
       
        canvas.drawText(mText, rec.width()/2, rec.height()/2+mTextSize/4, paint);  

	}
}
